package swing;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import engine.Control;
import engine.EventObserver;
import engine.GameRO;
import engine.View;

public class GamePanel extends JPanel implements View {

	private static final long serialVersionUID = 1L;
	private Image view;
	protected EventObserver<KeyEvent> keyObserver;
	protected EventObserver<MouseEvent> mouseObserver;

	public GamePanel(int width, int height, Control c) {
		setFocusable(true);
		requestFocus();
		setVisible(true);
		setSize(width, height);
		setPreferredSize(getSize());
		keyObserver = Observers.addKeyObserver(this, c);
		mouseObserver = Observers.addMouseObserver(this, c);
		addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {}

			@Override
			public void keyReleased(KeyEvent e) {}

			@Override
			public void keyPressed(KeyEvent e) {
				((GameFrame) getParent().getParent().getParent().getParent()).goFullScreen();
			}
		});
	}

	@Override
	public void update(GameRO game) {
		try {
			view = game.getCurrentRoom().getView();
			repaint();
		} catch (NullPointerException e) {}
	}

	@Override
	public void paint(Graphics g) {
		g.drawImage(view, 0, 0, getWidth(), getHeight(), this);
	}

}
